package com.fs.flow.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fs.base.pojo.PkEntity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * @author TiddlerCJ
 *
 */
@Entity
@Table(name="p_process_task_config")
@NoArgsConstructor
@AllArgsConstructor
@Data@EqualsAndHashCode(callSuper=false) 
public class ProcessTaskConfig  extends PkEntity{
	private String processKey;		//流程标识
	private String taskName;	//流程节点名称
	private String taskId;	//流程节点id
	private String taskType;	//流程节点类型
	
	private String nextChoose;	//下节点手动选择
	private String upRight;	  	//上传附件权限
	private String downRight;	//下载附件权限
	
	private String emailSwitch;	//邮件提醒
	private String smsSwitch;	//短信提醒
	private String submitName;	//提交方式
	
	private String backSwitch;	//回退开关
	private String endSwitch;	//终止开关
	
	private String roleReg;		//角色
	private String userReg;		//用户
	private String positionReg;	//岗位
	
	private String noticeUsers;	//通知人员
	
	@Column(columnDefinition="text COMMENT '表单json配置'")
	private String formJson;
	
	/**
	 * 获取隐藏属性字符串
	 * @return
	 */
	public String getHiddenField(){
		StringBuffer sb = new StringBuffer();
		try {
			if(StringUtils.isNotBlank(formJson)){
				JSONObject obj = JSON.parseObject(formJson);
				JSONArray rows = obj.getJSONArray("rows");
				JSONObject item = null;
				for(int i=0,len=rows.size(); i<len; i++){
					item = rows.getJSONObject(i);
					if(item.containsKey("inform") && !"Y".equals(item.getString("inform"))){
						sb.append(item.getString("field")).append("#");
					}
				}
			}
		} catch (Exception e) {
		}
		return sb.toString();
	}
	
	/**
	 * 获取不可编辑属性字符串
	 * @return
	 */
	public String getDisabledField(){
		StringBuffer sb = new StringBuffer();
		try {
			if(StringUtils.isNotBlank(formJson)){
				JSONObject obj = JSON.parseObject(formJson);
				JSONArray rows = obj.getJSONArray("rows");
				JSONObject item = null;
				for(int i=0,len=rows.size(); i<len; i++){
					item = rows.getJSONObject(i);
					if(item.containsKey("editable") && !"Y".equals(item.getString("editable"))){
						sb.append(item.getString("field")).append("#");
					}
				}
			}
		} catch (Exception e) {
		}
		return sb.toString();
	}
	
	
	/**
	 * 获取不必填辑属性字符串
	 * @return
	 */
	public String getRequiredField(){
		StringBuffer sb = new StringBuffer();
		try {
			if(StringUtils.isNotBlank(formJson)){
				JSONObject obj = JSON.parseObject(formJson);
				JSONArray rows = obj.getJSONArray("rows");
				JSONObject item = null;
				for(int i=0,len=rows.size(); i<len; i++){
					item = rows.getJSONObject(i);
					if(item.containsKey("required") && "Y".equals(item.getString("required"))){
						sb.append(item.getString("field")).append("#");
					}
				}
			}
		} catch (Exception e) {
		}
		return sb.toString();
	}
	
	
	/**
	 * 获取不流程变量属性字符串
	 * @return
	 */
	public String getVariableField(){
		StringBuffer sb = new StringBuffer();
		try {
			if(StringUtils.isNotBlank(formJson)){
				JSONObject obj = JSON.parseObject(formJson);
				JSONArray rows = obj.getJSONArray("rows");
				JSONObject item = null;
				for(int i=0,len=rows.size(); i<len; i++){
					item = rows.getJSONObject(i);
					if(item.containsKey("variable") && "Y".equals(item.getString("variable"))){
						sb.append(item.getString("field")).append("#");
					}
				}
			}
		} catch (Exception e) {
		}
		return sb.toString();
	}
}
